<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="服务编号" prop="code">-->
<!--        <el-input-->
<!--          v-model="queryParams.code"-->
<!--          placeholder="请输入服务编号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="服务项目" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入服务项目名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家名称" prop="shopName">
        <el-input
            v-model="queryParams.shopName"
            placeholder="请输入商家名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类型" prop="scName">
        <el-input
            v-model="queryParams.scName"
            placeholder="请输入服务类型"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="原价" prop="originalPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.originalPrice"-->
<!--          placeholder="请输入原价"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="现价" prop="currentPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.currentPrice"-->
<!--          placeholder="请输入现价"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable style="width: 200px">
          <el-option
            v-for="dict in merchant_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="逻辑删除：1-已删除，0-未删除" prop="isDeleted">-->
<!--        <el-input-->
<!--          v-model="queryParams.isDeleted"-->
<!--          placeholder="请输入逻辑删除：1-已删除，0-未删除"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['merchant:management:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['merchant:management:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:management:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['merchant:management:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="服务管理ID" align="center" prop="smId" />-->
      <el-table-column label="服务编号" align="center" prop="code" />
      <el-table-column label="服务项目名" align="center" prop="name" />
      <el-table-column label="商家名称" align="center" prop="shopName" />
      <el-table-column label="服务类型" align="center" prop="scName" />
      <el-table-column label="原价" align="center" prop="originalPrice" />
      <el-table-column label="现价" align="center" prop="currentPrice" />
      <el-table-column label="服务介绍" align="center" prop="intro" />
      <el-table-column label="服务内容" align="center" prop="content" />
      <el-table-column label="审核状态：" align="center" prop="auditStatus">
        <template #default="scope">
          <dict-tag :options="merchant_audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="逻辑删除：" align="center" prop="isDeleted" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['merchant:management:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['merchant:management:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="managementRef" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="服务编号" prop="code">-->
<!--          <el-input v-model="form.code" placeholder="请输入服务编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="服务项目名" prop="name">
          <el-input v-model="form.name" placeholder="请输入服务项目名" />
        </el-form-item>
        <el-form-item label="商家名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入商家名称" readonly />
<!--          <el-select v-model="form.mId" placeholder="请选择商家">-->
<!--            <el-option v-for="merchant in merchantList" :key="merchant.mId" :label="merchant.shopName" :value="merchant.mId" />-->
<!--          </el-select>-->
        </el-form-item>
        <el-form-item label="服务类型" prop="scName">
          <el-input v-model="form.scName" placeholder="请输入服务类型" readonly />
        </el-form-item>
        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="现价" prop="currentPrice">
          <el-input v-model="form.currentPrice" placeholder="请输入现价" />
        </el-form-item>
        <el-form-item label="服务介绍" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="服务内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="审核状态：" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态：">
            <el-option
              v-for="dict in merchant_audit_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逻辑删除：" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入逻辑删除：" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Management">
import { listManagement, getManagement, delManagement, addManagement, updateManagement } from "@/api/merchant/management"

const { proxy } = getCurrentInstance()
const { merchant_audit_status } = proxy.useDict('merchant_audit_status')

const managementList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    name: null,
    mId: null,
    shopName: null,
    scId: null,
    scName: null,
    originalPrice: null,
    currentPrice: null,
    intro: null,
    content: null,
    auditStatus: null,
    isDeleted: null
  },
  rules: {
    code: [
      { required: true, message: "服务编号不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "服务项目名不能为空", trigger: "blur" }
    ],
    // mId: [
    //   { required: true, message: "商家ID不能为空", trigger: "blur" }
    // ],
    shopName: [
      { required: true, message: "商家名称不能为空", trigger: "blur" }
    ],
    // scId: [
    //   { required: true, message: "服务分类ID不能为空", trigger: "blur" }
    // ],
    scName: [
      { required: true, message: "服务类型不能为空", trigger: "blur" }
    ],
    originalPrice: [
      { required: true, message: "原价不能为空", trigger: "blur" }
    ],
    currentPrice: [
      { required: true, message: "现价不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询服务管理列表 */
function getList() {
  loading.value = true
  listManagement(queryParams.value).then(response => {
    managementList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    smId: null,
    code: null,
    name: null,
    mId: null,
    shopName: null,
    scId: null,
    scName: null,
    originalPrice: null,
    currentPrice: null,
    intro: null,
    createTime: null,
    content: null,
    auditStatus: null,
    isDeleted: null
  }+
  proxy.resetForm("managementRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.smId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加服务管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _smId = row.smId || ids.value
  getManagement(_smId).then(response => {
    form.value = response.data
    form.value.shopName = response.data.shopName
    form.value.scName = response.data.scName  // 设置scName
    open.value = true
    title.value = "修改服务管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["managementRef"].validate(valid => {
    if (valid) {
      if (form.value.smId != null) {
        updateManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _smIds = row.smId || ids.value
  proxy.$modal.confirm('是否确认删除服务管理编号为"' + _smIds + '"的数据项？').then(function() {
    return delManagement(_smIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('merchant/management/export', {
    ...queryParams.value
  }, `management_${new Date().getTime()}.xlsx`)
}

getList()
</script>
